var rClickImg = 1;
var kxPos, kyPos;
var hsArea = false;
var qCtrl, segID;
var timeoutFocus;

currentSegmentId = "MSW_14000";
isFileMenu = !false;
DESIGN_MODE = false;

function loadQuestions(qctrl) {
    qCtrl = qctrl; 
    if (currentSegmentId == "MSW_14001") {
        isShowDropdown = !false;
		$("#glasscontainer").html('');
    } else if(currentSegmentId == "exit_result_1") {
		//isShowDropdown = !true;
	}
	
    drawHotspots();
}

function drawHotspots() {
    $('#questioncontainer').html('');
    var curItem, newItem;  
    
    for (z in currentSegmentData.htmls) {
        curItem = currentSegmentData.htmls[z];
        newItem = $(curItem[4]);
        newItem.css({
            position: "absolute",
            border: "0px",
            "z-index": 9,
            left: curItem[0] + "px",
            top: curItem[1] + "px",
            width: (curItem[2] - curItem[0]) + "px",
            height: (curItem[3] - curItem[1]) + "px"
        });
        
        $('#questioncontainer').append(newItem);
    }
    
    for (j in currentSegmentData.marks) {
        curItem = currentSegmentData.marks[j];
        newItem = $("<div></div>");
        newItem.attr("id", j);
        newItem.attr("onclick", "doClickHandlerInQues(event, this);correctClick=true;");
        newItem.attr("onmouseover", "doOverActionInQues(event, this)");
        newItem.attr("onmouseout", "doOutActionInQues(event, this)");
        newItem.bind("contextmenu", function(e) {
            actionOffice(event, this);
            return false;
        });
        
        newItem.css({"position": "absolute","display": "block","z-index": "2000","left": curItem[0] + "px","top": curItem[1] + "px","width": (curItem[2] - curItem[0]) + "px","height": (curItem[3] - curItem[1]) + "px"});
        if (j.substring(0, 1) == "s") {
            hsArea = true;
            newItem.css({"background-color": "red","opacity": "0"})
        }
        
        if (DESIGN_MODE) {
            newItem.css({"background-color": "red","opacity": "0.5"})
        }
        $('#questioncontainer').append(newItem);
    }
    for (var k in currentSegmentData.action) {
        actionType = k.substring(0, 1);
        
        if ('h'.indexOf(actionType) >= 0) {
            curItem = currentSegmentData.action[k];
            newItem = $("<div></div>");
            newItem.attr("id", k);
            newItem.css({
                display: "none",
                "background-color": (actionType == 'h' ? "LightSteelBlue" : ""),
                border: (actionType == 'b' ? "1px solid lightblue" : (actionType == 'B' ? "4px solid LightSteelBlue" : "0px")),
                'box-sizing': 'border-box',
                opacity: 0.5,
                position: "absolute",
                left: curItem[0] + "px",
                top: curItem[1] + "px",
                width: (curItem[2] - curItem[0]) + "px",
                height: (curItem[3] - curItem[1]) + "px"
            });
            $('#questioncontainer').append(newItem);
        }
    }
    
    //screenPage = i;

}

function doRightClickUsingKeys(event, ctrl) {
    kxPos = 450;
    kyPos = 210;
    doRightClickHandler(ctrl)
}

function doClickHandlerInQues(event, ctrl) {
    if (!isShowDropdown) {
        hsArea = false;
    }
    
    actionOffice(event, ctrl);
    
    if (event.target.id == "m_FileNameArrow") {
        incorrectMsg = true;
    }
}

function doOverActionInQues(event, ctrl) {
    log("gopal", "over=in q js.." + ctrl.id)    
	//clearKeyHighlightsInDropdown();
    doAllActionInQues(event, ctrl, 5);    
}

function doOutActionInQues(event, ctrl) {
    log("gopal", "out=in q js.." + ctrl.id)
    doAllActionInQues(event, ctrl, 5, true);
}

function doAllActionInQues(event, ctrl, idx, isActionRevert) {
	log("gopal", "doAllActionInQues: " + event.type);
	
    var actionList = currentSegmentData.marks[ctrl.id][idx];
	
	for (var i = 0; i < actionList.length; i++) {
		doAction(event, actionList[i], isActionRevert);
	}
}

var liItem, arrKeyCount = 0;

function doDropdownkeyHandler(keycode) {
	log("keyup", "doDropdownkeyHandler:: currentSegmentId:" + currentSegmentId);
	log("keyup", "doDropdownkeyHandler:: isShowDropdown:" + isShowDropdown);
	log("keyup", "doDropdownkeyHandler:: keycode:" + keycode);
	
    var curItem, liItem = [];	
    for (var i in currentSegmentData.marks) {
        curItem = i.substring(2)
        if (curItem != "dead")
            liItem.push(curItem)
    }
    
    if (keycode == 40) { //downArrow
        $("#m_" + liItem[arrKeyCount]).css({"background-color": "LightSteelBlue","opacity": "0"});
        arrKeyCount++;
    } else if (keycode == 38) { //upArrow		
        $("#m_" + liItem[arrKeyCount]).css({"background-color": "LightSteelBlue","opacity": "0"});
        arrKeyCount--;
        if (arrKeyCount < 0) {
            arrKeyCount = liItem.length;
        }
    }
	
    if (liItem.length > arrKeyCount) {
        $("#m_" + liItem[arrKeyCount]).css({"background-color": "LightSteelBlue","opacity": "0.5"});
    } else {
        if (keycode == 40) {
            arrKeyCount = 0;
        } else if (keycode == 38) {
            arrKeyCount = liItem.length - 1;
        }
        $("#m_" + liItem[arrKeyCount]).css({"background-color": "LightSteelBlue","opacity": "0.5"});
    }
    	
    //rightClickKeyHandler(keycode, event.ctrlKey, event.shiftKey, event.altKey, true, liItem[arrKeyCount])
	log("keyup", "doDropdownkeyHandler:: currentSegmentId:" + currentSegmentId);
	log("keyup", "doDropdownkeyHandler:: isShowDropdown:" + isShowDropdown);
	log("keyup", "doDropdownkeyHandler:: keycode:" + keycode);
}

function clearKeyHighlightsInDropdown() {	
    for (var i in currentSegmentData.marks) {
        curItem = i.substring(2)
        $("#m_" + curItem).css({"background-color": "LightSteelBlue","opacity": "0"});
    }
}

$(document).keyup(function(e) {
    if (currentSegmentId == "MSW_14030" || currentSegmentId == "MSW_14041") {
        isShowDropdown = true;
    } else {
        isShowDropdown = false;
    }
});

$(document).keydown(function(e) {    
    
    log("gopal", "==================> " + currentSegmentId + ", " + curPage + ", " + e.keyCode);
    log("gopal", "==================> " + isShowDropdown);
});

$(document).contextmenu(function(e) {
    
});

function onTextFieldClickHandler() {
    incorrectMsg = true;
}

function questionFileMenuMouse(event, dataId) {
    var bCorrect = !true;
    var actionType = dataId.substring(0, 1);

    // a used for alt keys
    if (actionType == 'd') {
        if (dataId == "d_\x12FAC" || dataId == "d_\x12FA2") {
            incorrectMsg = true;
            $(".keypanel").remove();
        }
    } else if (actionType == 'a') {
        if (dataId == "a_\x12FAB" || dataId == "a_\x12FA1" || dataId == "a_\x12FEA" || dataId == "a_\x12FEC") {
            bCorrect = true;
        }
    } else if (event.type == "click" && event.target.id == "m_Back") {
        bCorrect = true;
    }
    
    if (bCorrect == true) {
        isShowFileMenu = false;
        incorrectMsg = true;
        actionLoadPage(fileMenuPreviousTab);
    }
}

function questionFileMenuKey(strKey) {
    /* need to be implement: show the previous selected tab on exit the 
	file tab or clicking on any button/icon	*/
    
    switch (strKey) {
        case "a_\x12FAB":
        case "a_\x12FA1":
        case "a_\x12FEA":
        case "a_\x12FEC":
            return true;
            break;
    }
    
    return false;
}
